package com.tiger.cloud.controller.crm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.form.SysDictSmall;
import com.tiger.cloud.model.crm.Customer;
import com.tiger.cloud.service.crm.ICustomerService;
import com.tiger.cloud.service.system.CommonDict;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 客户
 */
@Controller
@RequestMapping(value = "customer")
public class CustomerController extends BaseController {

	private static final Logger logger=Logger.getLogger(CustomerController.class);
	
	@Autowired
	private ICustomerService customerService;
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public Response find(@CurrentUserId Long userId,HttpServletRequest request){
		Map<String, Object> params=getPageMap(request);
		Page<Customer> datas=customerService.page(params);
		return ResponseUtil.ok(datas);
	}
	
	/**
	 * 人脉圈-客户全量
	 * @param userId
	 * @param customer
	 * @return
	 */
	@IgnoreAuth
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "人脉圈-客户",notes = "人脉圈-客户")
	public Response list(@CurrentUserId Long userId,@RequestBody Customer customer){
		if (customer.getLastUpdateTime()==null) {
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID);
		}
		customer.setUserId(userId);
		return ResponseUtil.ok(customerService.find(customer));
	}
	
	/**
	 * 客户更新
	 * @param userId
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新客户",notes = "更新客户")
	public Response update(@CurrentUserId Long userId,@RequestBody Customer customer){
		customerService.update(customer);
		return ResponseUtil.ok();
	}
	
	/**
	 * 新增客户
	 * @param userId
	 * @param request
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "新增客户",notes = "新增客户")
	public Response add(@CurrentUserId Long userId,@RequestBody Customer customer){
		customer.setIsDelete(0);
		customer.setUserId(userId);
		customerService.insert(customer);
		return ResponseUtil.ok();
	}
	
	/**
	 * 删除客户
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除客户",notes = "删除客户")
	public Response delete(@RequestBody Customer customer){
		HashMap<String, Object> param=new HashMap<>();
		if(customer.getId()==null){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID);
		}
		param.put("id", customer.getId());
		customerService.remove(param);
		return ResponseUtil.ok();
	}
	
	/**
	 * 获取客户字典列表
	 */
	@IgnoreAuth
	@RequestMapping(value = "getDefaultFileds", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "获取客户字典列表",notes = "获取客户字典列表")
	public Response getDefaultFileds(){
		Map<String, List<SysDictSmall>> values = CommonDict.getFileds("loan_info");
		return ResponseUtil.ok(values);
	}
	
	/**
	 * 客户详情
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "load/{id}", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "客户详情",notes = "客户详情")
	public Response loadCustomer(@PathVariable Long id){
		return ResponseUtil.ok(customerService.get(id));
	}
	
	

}